package com.dony.web.controller.store;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dony.common.core.controller.BaseController;
import com.dony.common.core.domain.AjaxResult;
import com.dony.shop.domain.Discount;
import com.dony.shop.service.IDiscountService;
import com.dony.common.core.page.TableDataInfo;

/**
 * 折扣活动Controller
 *
 * @author sundw
 * @date 2022-08-31
 */
@RestController
@RequestMapping("/store/discount")
public class DiscountController extends BaseController {
    @Autowired
    private IDiscountService discountService;

    @PostMapping("/shop/{id}")
    public AjaxResult addPublishDiscount(@PathVariable("id") String id,
                                         @RequestParam(value = "a",required = false) String a,
                                         @RequestParam(value = "b",required = false) String b){
        System.out.println("id:"+id);
        return success();
    }

    /**
     * 同步商户的折扣活动
     */
    @PostMapping ("/sync")
    public AjaxResult sync(@RequestBody Discount discount) {
        return toAjax(discountService.syncByMerchantId(discount));
    }

    /**
     * 给产品添加折扣
     *
     * @param publishTaskIds
     * @return
     */
    @RequestMapping("/add/publish/{publishTaskIds}")
    public AjaxResult addPublishDiscount(@PathVariable("publishTaskIds") Long[] publishTaskIds){
        return toAjax(discountService.addPublishDiscount(publishTaskIds));
    }

    /**
     * 给店铺所有未添加折扣的产品添加折扣
     *
     * @return
     */
    @RequestMapping("/alldiscount/{merchantId}")
    public AjaxResult addAllDiscount(@PathVariable Long merchantId){
        return toAjax(discountService.addAllDiscount(merchantId));
    }



    /**
     * 给所有未添加折扣的产品添加折扣
     *
     * @param merchantId
     * @return
     */
    @RequestMapping("/merchant/discount/all/{merchantId}")
    public AjaxResult addDiscountAll(@PathVariable Long merchantId) {
        discountService.addDiscountAll(merchantId);
        return AjaxResult.success();
    }


    /**
     * 查询折扣活动列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Discount discount) {
        startPage();
        List<Discount> list = discountService.listDiscount(discount);
        return getDataTable(list);
    }

    /**
     * 获取折扣活动详细信息
     */
    @GetMapping(value = "/{discountId}")
    public AjaxResult getInfo(@PathVariable("discountId") Long discountId) {
        return AjaxResult.success(discountService.getById(discountId));
    }

    /**
     * 修改折扣活动
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Discount discount) {
        return toAjax(discountService.updateById(discount));
    }

    /**
     * 删除折扣活动
     */
    @DeleteMapping("/{discountIds}")
    public AjaxResult remove(@PathVariable Long[] discountIds) {
        return toAjax(discountService.removeByIds(Arrays.asList(discountIds)));
    }
}
